﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
using System.Configuration;
using ThanNhanTao.Server.BLL;
using ThanNhanTao.Server;
using ThanNhanTao.Properties;


namespace ThanNhanTao
{
    /// <summary>
    /// Interaction logic for frmLogin.xaml
    /// </summary>
    public partial class frmLogin : Window
    {
        public frmLogin()
        {
            bool wasConnected;
            try
            {
                InitializeComponent();
                // Get config information from app.config file
                string datasource = Settings.Default.DATASOURCE;
                string dbname = Settings.Default.DBNAME;
                string username = Settings.Default.USERNAME;
                string password = Settings.Default.PASSWORD;
                string connectionString = ConfigurationManager.ConnectionStrings["bvdn_thannhantaoEntities"].ToString();
                frmConnecSQL frmSQL = new frmConnecSQL();

                // Set connection from config file
                ThanNhanTaoGlobalServer.SetConnectionCommon(datasource, dbname, username, password, connectionString);
                if (!frmSQL.CheckConnectionServer())
                {
                    // Display configuration sreeen for setting connection
                    frmSQL.ShowDialog();
                    // Get flag was set from configuration sreeen
                    wasConnected = frmSQL.WasConnected;
                }
                else
                {
                    wasConnected = true;
                }

                // If application has connected yet and then end process
                if (!wasConnected) 
                {
                    // Close login screen
                    this.Close();
                    return;
                }

                CauHinhBLL cauHinhBll = new CauHinhBLL();

                // Check DB is existing
                if (!cauHinhBll.CheckExistingDatabase())
                {
                    // Display Backup and Restore form
                    frmSaoLuuPhucHoiDuLieu frmSaoLuuPhucHoi = new frmSaoLuuPhucHoiDuLieu();
                    frmSaoLuuPhucHoi.ShowDialog();
                    // Check restore function is action. If restore function is not action then close login form
                    if (!frmSaoLuuPhucHoi.isRestore)
                    {
                        this.Close();
                        return;
                    }
                }

                // Focus to username textbox login
                textBoxTen.Focus();
            }
            catch (Exception ex)
            {
                DXMessageBox.Show("Một lỗi xảy ra trên hệ thống \n" + ex.Message, "Thông báo lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                this.Close();
            }
        }

        private void imaDong_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void imaLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string userName = ConfigurationManager.AppSettings["UID"].ToString();
                string password = ConfigurationManager.AppSettings["PWD"].ToString();
                if (userName.Equals(textBoxTen.Text) && password.Equals(textBoxMatKhau.Text))
                {
                    GiaiDoanCho();
                    MainWindow main = new MainWindow();
                    this.Close();
                    main.ShowDialog();
                }
                else
                {
                    DXMessageBox.Show("Thông tin đăng nhập chưa đúng. Đăng nhập không thành công!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception)
            {
                DXMessageBox.Show("Có lỗi xảy ra. Cấu hình phần mềm chưa đúng!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void GiaiDoanCho()
        {
            this.blackLog.Cursor = Cursors.Wait;
            this.imaAnh.Cursor = Cursors.Wait;
            this.textBoxTen.Cursor = Cursors.Wait;
            this.textBoxMatKhau.Cursor = Cursors.Wait;
            this.imaDong.Cursor = Cursors.Wait;
            this.imaAnh.Cursor = Cursors.Wait;
            this.imaLamMoi.Cursor = Cursors.Wait;

        }

        private void imaLamMoi_Click(object sender, RoutedEventArgs e)
        {
            textBoxTen.Text = string.Empty;
            textBoxMatKhau.Text = string.Empty;
        }
    }
}
